﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CloudMask : MonoBehaviour
{
    // Start is called before the first frame update
    public RectTransform cloud0_transform;
    public RectTransform cloud1_transform;
    public GameObject cloud0;
    public GameObject cloud1;
    public string scene_name;
    public float speed;
    public int status;

    void Start()
    {
        cloud0 = GameObject.Find("Cloud0");
        cloud1 = GameObject.Find("Cloud1");
        Debug.Log(cloud0);
        cloud0_transform = cloud0.GetComponent<RectTransform>();
        cloud1_transform = cloud1.GetComponent<RectTransform>();
        status = -1;

        cloud0_transform.offsetMax = new Vector2(0, -150);
        cloud0_transform.offsetMin = new Vector2(0, -150);
        cloud1_transform.offsetMax = new Vector2(0, 150);
        cloud1_transform.offsetMin = new Vector2(0, 150);
        speed = 1000;

        scene_name = "zhen-test";
    }
    // Update is called once per frame
    void Update()
    {
        //Close();
        switch (status)
        {
            case -1:
                Open();
                break;
            case 1:
                Close(scene_name);
                break;
            default:
                break;
        }

    }

    void Close(string scene_name)
    {
        if (cloud0_transform.offsetMax.y < -150)
        {
            cloud0_transform.offsetMax = cloud0_transform.offsetMax + new Vector2(0, Time.deltaTime * speed);
            cloud0_transform.offsetMin = cloud0_transform.offsetMin + new Vector2(0, Time.deltaTime * speed);
            cloud1_transform.offsetMax = cloud1_transform.offsetMax - new Vector2(0, Time.deltaTime * speed);
            cloud1_transform.offsetMin = cloud1_transform.offsetMin - new Vector2(0, Time.deltaTime * speed);
            return;
        }
        else
        {
            status = 0;
            SceneManager.LoadScene(scene_name);
        }
    }

    void Open()
    {
        if (cloud0_transform.offsetMax.y > -1600)
        {
            cloud0_transform.offsetMax = cloud0_transform.offsetMax - new Vector2(0, Time.deltaTime * speed);
            cloud0_transform.offsetMin = cloud0_transform.offsetMin - new Vector2(0, Time.deltaTime * speed);
            cloud1_transform.offsetMax = cloud1_transform.offsetMax + new Vector2(0, Time.deltaTime * speed);
            cloud1_transform.offsetMin = cloud1_transform.offsetMin + new Vector2(0, Time.deltaTime * speed);
            return;
        }
        else
        {
            status = 0;
        }

    }
}
